﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CoderForRent.Silverlight.GanttExample;
using Gantt.TaskService;
using System.Collections.ObjectModel;
using CoderForRent.Silverlight.Charting.Gantt;

namespace Gantt
{
    public partial class MainPage : UserControl
    {
        public MainPage(int id)
        {
            InitializeComponent();
            gantt.GeneratingGanttPanelColumn += new CoderForRent.Silverlight.Charting.Gantt.GanttPanelColumnHandler(gantt_GeneratingGanttPanelColumn);
            gantt.ColumnGeneratingType = typeof(ExampleGanttNode);
            _id = id;   //获取房子id信息
            gantt.Loaded += gantt_Loaded;   //用于加载xaml文件
        }
        void gantt_GeneratingGanttPanelColumn(object sender, CoderForRent.Silverlight.Charting.Gantt.GanttPanelColumnEventArgs e)
        {
            bool important = false;

            //如果是月的第一天则将该月设置为灰色
            if (e.Column.RepresentedDate.Day == 1)
            {
                e.Column.BorderThickness = new Thickness(1, 0, 0, 0);
                e.Column.BorderBrush = new SolidColorBrush(Colors.Gray);
                important = true;
            }

            //如果是周末设置成灰色. 
            if (e.Column.RepresentedDate.DayOfWeek == DayOfWeek.Saturday || e.Column.RepresentedDate.DayOfWeek == DayOfWeek.Sunday)
            {
                e.Column.Background = new SolidColorBrush(Colors.LightGray);
                important = true;
            }

            if (!important)
            {
                e.Column.Background = new SolidColorBrush(Colors.Transparent);
                e.Cancel = true;
            }
        }
        public int _id { set; get; }

        //此处用来调用WCF的相关服务
        private void gantt_Loaded(object sender, RoutedEventArgs e)
        {
           TaskServiceClient client = new TaskServiceClient();

            client.GetTasksCompleted+= client_GetTasksCompleted;  //注册事件

            //调用服务器端方法将房间id传入
            client.GetTasksAsync(_id);

        }
        private void client_GetTasksCompleted(object sender, GetTasksCompletedEventArgs e)
        {
            ObservableCollection<Task> tasks = e.Result;
            ObservableCollection<IGanttNode> nodes = new ObservableCollection<IGanttNode>();
            foreach (Task t in tasks)
                nodes.Add(ConvertTaskToNode(t));   //用于添加结点

            gantt.Nodes = nodes;
        }

        //递归返回项目中的结点
        private ExampleGanttNode ConvertTaskToNode(Task t)
        {
            ExampleGanttNode result = new ExampleGanttNode
            {
                TaskName = t.TaskName,    //工程名
                StartDate = t.StartDate,    //工程开始时间
                EndDate = t.EndDate,    //工程结束时间
                Resources = t.Resources,   //工程所需材料
                PercentComplete = t.PercentComplete,   //完成百分比
                Expanded = false                   //表示折叠

            };

            if (t.Children != null)
            {
                //向甘特图中添加结点
                foreach (Task child in t.Children)
                {
                    result.ChildNodes.Add(ConvertTaskToNode(child));
                }
            }

            return result;
        }
    }
}
